package com.eeepay.eeepay_v2.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.AgentListAdapter;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.AgentApiServiceGrpc;
import cn.eeepay.api.grpc.nano.AgentSerProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/11 0011.
 * 代理商列表
 */
public class AgentListActivity extends ABBaseActivity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener {

    private TitleBar titleBar;
    private ListView lv_content;

    private String agentName, startTime, endTime;
    private boolean hasChild;

    private AgentListAdapter adapter;

    private int lastItem;
    private int page = 1;
    private int pageSize = 10;
    private int total;//总数量

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent_list;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        lv_content = getViewById(R.id.lv_content);
        adapter = new AgentListAdapter(mContext);

        if(bundle != null){
            agentName = bundle.getString(Constant.AGENT_NAME);
            startTime = bundle.getString(Constant.CREATE_START_TIME);
            endTime = bundle.getString(Constant.CREATE_END_TIME);
            hasChild = bundle.getBoolean(Constant.HAS_CHILD);
        }


        lv_content.setAdapter(adapter);

        sendHttpRequest(0);

    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();

        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT
                , taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                AgentApiServiceGrpc.AgentApiServiceBlockingStub stub = AgentApiServiceGrpc
                        .newBlockingStub(channel)
                        .withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);



                AgentSerProto.ReqByName request = new AgentSerProto.ReqByName();
                request.curAgentNo = UserInfo.getUserInfo2SP().getAgentNo();
                request.agentName = agentName;
                request.createStartDate = startTime;
                request.createEndDate = endTime;
                request.hasChild = hasChild;

                request.page = page;
                request.size = pageSize;

                return stub.queryAgentInfoList(request);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if(result == null){
                    showToast("查询失败");
                    return;
                }
                AgentSerProto.AgentBaseInfoListApi response = (AgentSerProto.AgentBaseInfoListApi) result;
                LogUtils.d("TAG", "size = " + response.agentBaseInfolist.length);
                total = response.total;
                if(page == 1){
                    adapter.setList(Arrays.asList(response.agentBaseInfolist));
                }else{
                    adapter.addAll(Arrays.asList(response.agentBaseInfolist));
                }
            }
        });
    }

    @Override
    protected void eventOnClick() {
        lv_content.setOnItemClickListener(this);
        lv_content.setOnScrollListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.AGENT_ID, adapter.getItem(position).agentNo);
        bundle.putString(Constant.AGENT_NAME, adapter.getItem(position).agentName);
        bundle.putString(Constant.ISDIRECTCHILD, adapter.getItem(position).isDirectChild);//add by zhuangzeqin 新增字段是否为直接下级 "true" : 是; "false"
        LogUtils.d("TAG", "agentName = " + adapter.getItem(position).agentName);
        goActivity(AgentDetailsActivity.class, bundle);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (lastItem == adapter.getCount()
                && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            page++;
            LogUtils.d("TAG", "page = " + page);
            if((page - 1) * pageSize < total){
                sendHttpRequest(0);
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        lastItem = firstVisibleItem + visibleItemCount;
    }
}
